/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.ui.util;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.PixelConverter;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

/**
 * @author Sean W. Quinn (swquinn@gmail.com)
 */
public final class ControlUtils {

  /**
   * The default visible item count for {@link Combo}s.
   * @see Combo#setVisibleItemCount(int)
   */
  public static final int COMBO_VISIBLE_ITEM_COUNT= 30;

  private ControlUtils() {
    // Empty. Do not instantiate this class!
  }

  /**
   *
   * @param composite
   * @param text
   * @param style
   * @return
   */
  public static Button createButton(Composite composite, String text, final int style) {
    final Button button = new Button(composite, SWT.PUSH);
    button.setFont(composite.getFont());
    button.setText(text);

    final GridData gd = new GridData(style);
    gd.widthHint = getButtonWidthHint(button);
    button.setLayoutData(gd);
    return button;
  }


  /**
   *
   * @param parent
   * @param numColumns
   * @return
   */
  public static Composite createComposite(Composite parent, int numColumns) {
    final Composite composite = new Composite(parent, SWT.NONE);
    composite.setFont(parent.getFont());

    final GridLayout layout = new GridLayout(numColumns, false);
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    composite.setLayout(layout);
    return composite;
  }

  /**
   * <p>
   * Creates
   *
   * @param composite
   * @param text
   * @param numColumns
   * @return
   */
  public static Label createLabel(Composite composite, String text, int numColumns) {
    final GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gd.horizontalSpan = numColumns;
    gd.widthHint = 0;

    final Label label = new Label(composite, SWT.WRAP);
    label.setFont(composite.getFont());
    label.setText(text);
    label.setLayoutData(gd);
    return label;
  }

  /**
   * Returns a width hint for a button control.
   *
   * @param button the button
   * @return the width hint
   */
  public static int getButtonWidthHint(Button button) {
    button.setFont(JFaceResources.getDialogFont());
    PixelConverter converter = new PixelConverter(button);
    int widthHint = converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
    return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
  }

  /**
   * Sets the default visible item count for {@link Combo}s. Workaround for
   * https://bugs.eclipse.org/bugs/show_bug.cgi?id=7845 .
   *
   * @param combo the combo
   *
   * @see Combo#setVisibleItemCount(int)
   * @see #COMBO_VISIBLE_ITEM_COUNT
   *
   * @since 3.5
   */
  public static void setDefaultVisibleItemCount(Combo combo) {
    combo.setVisibleItemCount(COMBO_VISIBLE_ITEM_COUNT);
  }

  /*
   * Convenience method to create a group
   */
  protected Group createGroup(final Composite parent, final String text, final PixelConverter pixelConverter, final int numColumns) {
    final Group group= new Group(parent, SWT.NONE);
    group.setFont(parent.getFont());
    //group.setLayoutData(createGridData(numColumns, GridData.FILL_HORIZONTAL, SWT.DEFAULT));

    final GridLayout layout= new GridLayout(numColumns, false);
    layout.verticalSpacing=  pixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
    layout.horizontalSpacing= pixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
    layout.marginHeight=  pixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);

    //layout.marginHeight= fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
    //layout.marginWidth= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);

    group.setLayout(layout);//createGridLayout(numColumns, true));
    group.setText(text);
    return group;
  }

}
